<template>
  <div class="register">
    <div class="register-form">
      <div><h3>企业注册</h3></div>
      <el-form
        :model="addBusinessForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item
          label="企业编号"
          prop="enterpriseNumber"
          class="classform"
        >
          <el-input v-model="addBusinessForm.enterpriseNumber"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addBusinessForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="addBusinessForm.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="addBusinessForm.leader"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="addBusinessForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="企业邮箱" prop="email">
          <el-input v-model="addBusinessForm.email"></el-input>
        </el-form-item>
        <el-row>
          <el-button type="success" class="zhuche" @click="regBusiness"
            >注册</el-button
          >
          <el-button type="info" class="zhuche" @click="backlogin"
            >已有账号</el-button
          >
          <el-button type="primary" class="chongzhi" @click="resetBusinessForm"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的邮箱"));
    };
    //验证电话号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //注册表单验证规则
      addFormRules: {
        //验证编号是否合法
        enterpriseNumber: [
          { required: true, message: "请输入编号", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        enterpriseName: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        leader: [
          { required: true, message: "请输入负责人", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur",
          },
        ],
      },
      addBusinessForm: {
        enterpriseNumber: "",
        password: "",
        enterpriseName: "",
        leader: "",
        phone: "",
        email: "",
      },
    };
  },
  methods: {
    backlogin() {
      this.$router.push("/login");
    },
    //点击重置按钮，重置登录表单
    resetBusinessForm() {
      this.$refs.addFormRef.resetFields();
    },
    //注册提交按钮
    regBusiness() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/enterpriseRegister",
          this.addBusinessForm
        );
        if (res.code !== 205)
          return this.$message.error("注册失败！此企业编号已被注册！");
        this.$message.success("注册成功!");
        this.$refs.addFormRef.resetFields();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.register {
  background-color: aquamarine;
  height: 753px;
  position: relative;
  .register-form {
    width: 400px;
    height: 500px;
    background-color: #eee;
    font-size: 16px;
    position: absolute;
    top: 120px;
    left: 560px;
    h3 {
      margin-top: 30px;
    }
    .zhuche {
      float: left;
      margin-left: 45px;
    }
    .chongzhi {
      float: right;
      margin-right: 35px;
    }
    .el-form {
      .el-form-item {
        padding: 0;
        width: 350px;
        font-size: 11px;
        margin: 20px 35px 20px 15px;

        .el-select {
          margin-left: 0px;
        }
      }
    }
  }
}
</style>